package cn.huoyingkeji.boot;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.baomidou.mybatisplus.core.metadata.IPage;

import cn.huoyingkeji.common.domain.DTO;
import cn.huoyingkeji.common.domain.Page;
import cn.huoyingkeji.common.domain.R;
import cn.huoyingkeji.common.exception.SharpknifeException;
import cn.huoyingkeji.common.service.BaseService;
import io.swagger.v3.oas.annotations.Operation;

public abstract class BaseController <D extends DTO,Service extends BaseService<D>> {
    
    /**
     * 设置controller名称
     * @return
     */
    public void setControllerName(String controllerName){
        this.controllerName = controllerName;
    }
    
    private String controllerName = "";

    @Autowired
    protected Service service;

    /**
     * 添加
     */
    @Operation(summary = "添加")
    @PostMapping("/add")
    public R<D> add(@RequestBody D dto) throws SharpknifeException{
        return R.data(service.add(dto));
    }

    /**
     * 修改
     */
    @Operation(summary = "修改")
    @PostMapping("/update")
    public R<D> update(@RequestBody D dto) throws SharpknifeException{
        return R.data(service.update(dto));
    }


    /**
     * 删除
     * @param id
     * @return
     * @throws SharpknifeException
     */
    @Operation(summary = "根据ID删除")
    @DeleteMapping("/byId/{id}")
    public R<Object> del(@PathVariable Long id) throws SharpknifeException{
        return R.status(service.del(id));
    }


    /**
     * 删除
     * @param ids
     * @return
     * @throws SharpknifeException
     */
    @Operation(summary = "根据ID的集合删除多个")
    @DeleteMapping("/byIds")
    public R<Object> del(@RequestBody Set<Long> ids) throws SharpknifeException{
        return R.status(service.del(ids));
    }



    /**
     * 详情
     * @param id
     * @return
     * @throws SharpknifeException
     */
    @Operation(summary = "获取详情")
    @GetMapping("/details/{id}")
    public R<D> details(@PathVariable Long id) throws SharpknifeException{
        return R.data(service.details(id));
    }


    /**
     * 分页查询
     * @param pageDTO
     * @return
     * @throws SharpknifeException
     */
    @Operation(summary = "分页查询")
    @GetMapping("/page")
    public R<IPage<D>> page(Page page,D dto) throws SharpknifeException{
        return R.data(service.page(page,dto));
    }


    /**
     * 查询
     * @param dto
     * @return
     * @throws SharpknifeException
     */
    @Operation(summary = "查询所有")
    @GetMapping("/list")
    public R<List<D>> all(D dto) throws SharpknifeException{
        return R.data(service.all(dto));
    }

}
